package com.example.canteen.controller;

import com.example.canteen.Response;
import com.example.canteen.dto.CategoryDTO;
import com.example.canteen.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @PostMapping("/all")
    public Response<Set<CategoryDTO>> getAllCategories() {
        return Response.newSuccess(categoryService.getAllCategories());
    }

    @PostMapping("/add")
    public Response<Long> addCategory(@RequestBody CategoryDTO categoryDTO) {
        return Response.newSuccess(categoryService.addCategory(categoryDTO));
    }

    @PostMapping("/delete")
    public void deleteCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.deleteCategoryByName(categoryDTO.getName());
    }

    @PostMapping("/update")
    public Response<CategoryDTO> updateCategory(@RequestBody CategoryDTO categoryDTO) {
        return Response.newSuccess(categoryService.updateCategory(categoryDTO.getId(), categoryDTO.getName()));
    }
}
